package com.example.demo.demos.web.wx;

import com.thoughtworks.xstream.XStream;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

/**
 * @Author：zhanghao
 * @name：WinxinController
 * @Date：2025/5/27 10:03
 */
@RestController
@Slf4j
public class WinxinController {

    private final String TOKEN = "ZS2TTBJt"; // 自定义的Token


    @PostMapping("/weixin")
    public String handleMessage(ServletRequest request, ServletResponse response) throws IOException {
// 数据提取
        WxUserMessageModel wxUserMessageModel = resolveXmlData(request.getInputStream());
        log.info("wxUserMessageModel:" + wxUserMessageModel);
        if (null == wxUserMessageModel) {
            // 不回消息也需要返回"success"
            return "success";
        }
        // 返回数据
        WxUserMessageModel responseXmlData = new WxUserMessageModel();
        responseXmlData.setToUserName(wxUserMessageModel.getFromUserName());
        responseXmlData.setFromUserName(wxUserMessageModel.getToUserName());
        responseXmlData.setCreateTime(new Date().getTime());
        responseXmlData.setMsgType("text");
        // 按需编写业务
        responseXmlData.setContent(wxUserMessageModel.getContent());

        XStream xstream = new XStream();
        xstream.processAnnotations(WxUserMessageModel.class);
        xstream.setClassLoader(WxUserMessageModel.class.getClassLoader());
        return xstream.toXML(responseXmlData);  //XStream的方法，直接将对象转换成 xml数据
    }


    @GetMapping("/weixin")
    public String verifyToken(@RequestParam("signature") String signature,
                              @RequestParam("timestamp") String timestamp,
                              @RequestParam("nonce") String nonce,
                              @RequestParam("echostr") String echostr) {
        log.info("verifyToken");
        log.info("signature:" + signature);
        log.info("timestamp:" + timestamp);
        log.info("nonce:" + nonce);
        log.info("echostr:" + echostr);
        if (SignUtil.checkSignature(signature, TOKEN, timestamp, nonce)) {
            return echostr; // 验证成功，返回echostr
        }
        return "error"; // 验证失败
    }

    /**
     * 解析xml数据
     *
     * @param in 输入流
     * @return 微信用户信息实体类
     * @throws IOException
     */
    private WxUserMessageModel resolveXmlData(InputStream in) throws IOException {
        String xmlData = IOUtils.toString(in);
        WxUserMessageModel wxXmlData = null;
        XStream xstream = new XStream();
        // 设置加载类的类加载器
        xstream.setClassLoader(WxUserMessageModel.class.getClassLoader());
        xstream.processAnnotations(WxUserMessageModel.class);
        xstream.alias("xml", WxUserMessageModel.class);
        wxXmlData = (WxUserMessageModel) xstream.fromXML(xmlData);
        return wxXmlData;
    }


}
